<?php
$cs = Yii::app()->clientScript;
$cs->registerScriptFile(Yii::app()->theme->baseUrl.'/scripts/CodeMirror/js/codemirror.js');

$script = "
jQuery.editor = CodeMirror.fromTextArea('".CHtml::getActiveId($templateContent, 'html')."', {
    height: '350px',
    parserfile: ['parsexml.js','parsejavascript.js','tokenizejavascript.js','parsehtmlmixed.js','parsecss.js','../contrib/php/js/parsephp.js','../contrib/php/js/tokenizephp.js','../contrib/php/js/parsephphtmlmixed.js'],
    stylesheet: '".Yii::app()->theme->baseUrl."/scripts/CodeMirror/css/xmlcolors.css',
    path: '".Yii::app()->theme->baseUrl."/scripts/CodeMirror/js/',
    continuousScanning: 500,
    lineNumbers: true
});
";
$cs->registerScript(__CLASS__.'#InitXMLEditor', $script);

$script = "
jQuery('#Workflow_master_layout_revision').change(function(){
    var master_layout_revision = jQuery(this).val();
    if (master_layout_revision.length > 0)
    {
        var url = '".$this->createUrl('', array('id'=>$workflow->id))."';
        jQuery.get(url, {master_layout_revision : master_layout_revision}, function(res){
            res = jQuery(res).find('#TemplateContent_html');
            jQuery.editor.setCode(res.val());
        });
    }
    else
    {
        jQuery.editor.setCode('');
    }
});
";
$cs->registerScript(__CLASS__.'#ChangeContent', $script);

$script = "
jQuery('#new_revision_checkbox').click(function(){
    if (jQuery(this).attr('checked'))
    {
        jQuery('.new-revision-log').show();
    }
    else
    {
        jQuery('.new-revision-log').hide();
    }
});
";
$cs->registerScript(__CLASS__.'#NewRevision', $script);
if ($newRevision)
{
    $cs->registerScript(__CLASS__.'#NewRevisionTrigger', "jQuery('.new-revision-log').show();");
    if ($templateContent->hasErrors('log'))
    {
        $cs->registerScript(__CLASS__.'#SelectTab', "setTimeout(function(){jQuery('#workflow-tabs').tabs('select', 1);}, 500);");
    }
}
?>
<style type="text/css">
  .CodeMirror-line-numbers {
    width: 2.2em;
    color: #aaa;
    background-color: #eee;
    text-align: right;
    padding-right: .3em;
    font-size: 10pt;
    font-family: monospace;
    padding-top: .4em;
    line-height: normal;
  }
</style>
<?php echo $form->hiddenField($template, 'name');?>
<?php echo $form->hiddenField($template, 'group');?>

<?php if (count($masterLayouts)):?>
<div class="row">
    <?php echo $form->labelEx($workflow,'master_layout_revision', array('label'=>'Revision')); ?>
    <?php echo $form->dropDownList($workflow, 'master_layout_revision', $masterLayouts) ?>
</div>
<?php endif;?>

<div class="row">
    <?php echo $form->labelEx($templateContent, 'html', array('label'=>'HTML')); ?>
</div>

<div class="row" style="border: 1px solid black; margin-left: 210px;">
    <?php echo $form->textArea($templateContent, 'html', array('cols'=>70, 'rows'=>7, 'encode'=>true)); ?>
</div>

<?php //if ($workflow->id):?>
<div class="row">
    <label>&nbsp;</label>
    <?php echo CHtml::hiddenField('new_revision', 0); ?>
    <?php echo CHtml::checkBox('new_revision', $newRevision, array('id'=>'new_revision_checkbox')); ?>
    <label for="new_revision_checkbox" style="float:none;width: auto;text-align:left; display:inline">Check this box to generate new revision of this master layout.</label>
</div>
<div class="row new-revision-log" style="display: none;">
    <?php echo $form->labelEx($templateContent, 'log'); ?>
    <?php echo $form->textArea($templateContent, 'log', array('cols'=>70, 'rows'=>7)); ?>
    <?php echo $form->error($templateContent,'log'); ?>
</div>
<?php //endif;?>